package cn.afactory.tygz.ui.sns.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.harwkin.nb.camera.BaseImgView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

import java.util.ArrayList;
import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsTopicInfo;
import cn.afactory.tygz.model.SnsUserInfo;
import cn.afactory.tygz.ui.base.utils.SPUtils;
import cn.afactory.tygz.ui.sns.TopicListActivity;

/**
 *
 */
public class TopicListAdapter extends BaseAdapter {
    private Context mContext;
    private List<SnsTopicInfo> mList = new ArrayList<SnsTopicInfo>();
    /**
     * 布局转换器
     */
    private LayoutInflater inflater;
    private DisplayImageOptions options;
    private int mSelectType ;
    public TopicListAdapter(Context context, List<SnsTopicInfo> list,int selectType) {
        this.inflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mList = list;
        this.mSelectType = selectType;
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_pic_loading_default_small)
                .showImageForEmptyUri(R.drawable.ic_pic_loading_default_small)
                .showImageOnFail(R.drawable.ic_pic_loading_default_small)
                .cacheInMemory(true).cacheOnDisk(true).considerExifParams(true)
                .build();

    }

    public void setData(List<SnsTopicInfo> list) {
        this.mList = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final SnsTopicInfo snsTopicInfo = mList.get(position);
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_topic_list, null);
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.hc_show_pic);
            viewHolder.titleTv = (TextView) convertView.findViewById(R.id.hc_title);
            viewHolder.descTv = (TextView) convertView.findViewById(R.id.hc_desc);
            viewHolder.btn_attention = (TextView) convertView.findViewById(R.id.btn_attention);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (!TextUtils.isEmpty(snsTopicInfo.topicName)) {
            viewHolder.titleTv.setText(snsTopicInfo.topicName);
        } else {
            viewHolder.titleTv.setText("");
        }
        if (!TextUtils.isEmpty(snsTopicInfo.desc)) {
            viewHolder.descTv.setVisibility(View.VISIBLE);
            viewHolder.descTv.setText(snsTopicInfo.desc);
        } else {
            viewHolder.descTv.setVisibility(View.GONE);
            viewHolder.descTv.setText("");
        }

        if (mSelectType == TopicListActivity.TYPE_CHOOSE_TOPIC_ID) {
            viewHolder.btn_attention.setVisibility(View.GONE);
        } else {
            if (SPUtils.isSelf(mContext, snsTopicInfo.createId)) {
                viewHolder.btn_attention.setVisibility(View.GONE);
            } else {
                viewHolder.btn_attention.setVisibility(View.VISIBLE);
                if (snsTopicInfo.isAttentioned) {
                    viewHolder.btn_attention.setText(mContext.getString(R.string.label_btn_cancel_attention));
                } else {
                    viewHolder.btn_attention.setText(mContext.getString(R.string.label_btn_attention));
                }

                viewHolder.btn_attention.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!snsTopicInfo.isAttentioned) {
                            //TODO关注
                            mOnItemClickListener.OnAttentionClick((TextView) v, snsTopicInfo);
                        }else {
                            //TODO取消关注
                            mOnItemClickListener.onCancelAttentionClick((TextView) v, snsTopicInfo);
                        }
                    }
                });
            }
        }

        if (!TextUtils.isEmpty(snsTopicInfo.topicIcon)) {
            viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            BaseImgView.loadimg(viewHolder.imageView,
                    snsTopicInfo.topicIcon,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    R.drawable.ic_pic_loading_default_small,
                    ImageScaleType.EXACTLY,
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    (int) mContext.getResources().getDimension(R.dimen.healthcircle_padding30),
                    0);
        } else {
            viewHolder.imageView
                    .setImageResource(R.drawable.ic_pic_loading_default_small);
        }
        return convertView;
    }

    OnItemClickListener mOnItemClickListener;
    public void setOnItemClickListener(OnItemClickListener lsn){
        mOnItemClickListener = lsn;
    }

    public interface OnItemClickListener{
        void OnAttentionClick(TextView v,SnsTopicInfo snsTopicInfo);

        void onCancelAttentionClick(TextView v,SnsTopicInfo snsTopicInfo);
    }

    private class ViewHolder {
        private ImageView imageView;
        private TextView btn_attention;
        private TextView titleTv;
        private TextView descTv;
    }
}